package com.myapp.struts.action;

import com.myapp.hibernate.model.Combate;
import com.myapp.hibernate.util.CombateUtil;
import com.myapp.struts.form.CombatesForm;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class GuardarNuevoCombateAction extends org.apache.struts.action.Action {

    private static final String SUCCESS = "success";

    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        CombatesForm formulario = (CombatesForm) form;
        CombateUtil combateUtil = new CombateUtil();

        float cuota1 = Float.parseFloat(request.getParameter("cuota1"));
        float cuota2 = Float.parseFloat(request.getParameter("cuota2"));
        int idB1 = Integer.parseInt(formulario.getBoxeador1());
        int idB2 = Integer.parseInt(formulario.getBoxeador2());
        float precio = Float.parseFloat(request.getParameter("precio"));
        int idEstadio = Integer.parseInt(formulario.getEstadio());

        SimpleDateFormat formatoDelTexto = new SimpleDateFormat("yyyy-MM-dd");
        String strFecha = request.getParameter("anyo") + "-" + request.getParameter("mes") + "-" + request.getParameter("dia");
        Date fecha = null;
        try {
            fecha = formatoDelTexto.parse(strFecha);
        } catch (Exception ex) {
            ex.printStackTrace();
        }


        Combate c = new Combate(idB1, idB2, idEstadio, precio, fecha, cuota1, cuota2);
        combateUtil.guardar(c);

        return mapping.findForward(SUCCESS);
    }
}
